package com.cmwa.solution.utils;

import java.text.DecimalFormat;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.cmwa.solution.sys.dictionary.entity.DictionaryVo;
import com.cmwa.solution.utils.cached.DictionaryCached;

/**
 * 系统工具类
 * 
 * @author ex-hejm
 * @date 2018年10月12日 上午11:22:39
 */
public class Utils {
	/**
	 * 转换特殊字符
	 * 
	 * @param special
	 * @return
	 */
	public static String changeSpecialCharacter(String special) {
		if (StringUtils.isNotEmpty(special)) {
			List<DictionaryVo> type = DictionaryCached.getDictionaryByType("SPECIAL_CHARACTER");
			if (CollectionUtils.isNotEmpty(type)) {
				for (DictionaryVo dictionaryVo : type) {
					if (special.contains(dictionaryVo.getDctName())) {
						special = special.replaceAll(dictionaryVo.getDctName(), dictionaryVo.getDctValue());
					}
				}
			}
		}
		return special;
	}

	/**
	 * 中文字符真实长度
	 * @author ex-hejm
	 * @date 2018年10月30日 下午1:49:35
	 * @param value
	 * @return
	 */
	public static int chineseLength(String value) {
		if(StringUtils.isBlank(value)) {
			return 0;
		}
		int valueLength = 0;
		String chinese = "[\u4e00-\u9fa5]";
		for (int i = 0; i < value.length(); i++) {
			String temp = value.substring(i, i + 1);
			if (temp.matches(chinese)) {
				valueLength += 2;
			} else {
				valueLength += 1;
			}
		}
		return valueLength;
	}
	
	/**
	 * oracle中的lpad函数，前置补零
	 * @param num
	 * @param size
	 * @return
	 */
	public static String lpadFillByZero(int num, int size) {
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < size; i++) {
			sb.append("0");
		}
		DecimalFormat df = new DecimalFormat(sb.toString());
		return df.format(num);
	}
	
	/**
	 * 生成不重复的code，重复截取最后2位数字，并加1
	 * @author ex-hejm
	 * @date 2019年2月21日下午3:06:05
	 * @param oldStr
	 * @param numLen
	 * @return
	 */
	public static String generateCodePinyinStr(String oldStr) {
		String pinYin = oldStr;
		if(StringUtils.isNotBlank(oldStr) && oldStr.length() > 2) {
			String numStr = oldStr.substring(oldStr.length() - 2);
			if(!numStr.matches("[a-zA-Z]+")) {
				int num = Integer.parseInt(numStr);
				num = num + 1;
				String dbCount = "";
				if(num < 10) {
					dbCount = "0" + num;
				}
				pinYin = oldStr.substring(0, oldStr.length() - 2) + dbCount;
			} else {
				pinYin = oldStr + "01";
			}
		}
		return pinYin;
	}
	
	public static void main(String[] args) {
		System.out.println(generateCodePinyinStr("aaa"));
	}
}
